package com.zzptc.zzsgz.bdsjws.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpdate;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class CheckUpdateActivity extends BaseActivity {
    ImageView picOne;
    ImageView picTwo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_check_update;
    }

    @Override
    public void initData() {
        boolean b = checkNet();
        if(b){
            
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://119.29.227.211/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
           IVersionUpdate versionUpdate = retrofit.create(IVersionUpdate.class);
            Call<VersionInfo> versionInfoCall = versionUpdate.getVersion();
            versionInfoCall.enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    //网络正常
                    VersionInfo versionInfo = response.body();
                    int netcode = versionInfo.getVersionCode();
                    int oldcode = MyUtil.getVersionCode(MyApplication.getContext());
                    if (netcode > oldcode){
                        //网络正常可以下载
//                        setResult(3);
                        Intent intent = new Intent();
                        intent.putExtra("VersionInfo" , versionInfo);
                        setResult(3 , intent);
                        finish();
                    }else{
                        //当前是最新版本
                        setResult(4);
                        Toast.makeText(CheckUpdateActivity.this, "当前是最新版本", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    //网络比较慢
                    setResult(1);
                    finish();
                }
            });

        }else{
            setResult(2);
            finish();
        }
    }




        public boolean checkNet(){
            NetworkInfo.State wifiState = null;
            NetworkInfo.State mobileState = null;
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
            if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                    && NetworkInfo.State.CONNECTED == mobileState) {
                // 手机网络连接成功
                Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
                return true;
            } else if (wifiState != null && mobileState != null
                    && NetworkInfo.State.CONNECTED != wifiState
                    && NetworkInfo.State.CONNECTED != mobileState) {
                // 手机没有任何的网络
                Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
                return false;
            } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
                // 无线网络连接成功
            //    Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }


    @Override
    public void initView() {
        picOne = (ImageView) findViewById(R.id.pic_one);
        picTwo = (ImageView) findViewById(R.id.pic_two);
        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(picOne , "scaleY" , 1f,100f,1f);
        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(picOne , "scaleX" , 1f,100f,1f);
        final AnimatorSet set = new AnimatorSet();
        set.play(objectAnimatorX).with(objectAnimatorY);
        set.setDuration(3000);
        set.start();



        ObjectAnimator objectAnimatorY1 = ObjectAnimator.ofFloat(picTwo , "scaleY" , 1f,100f,1f);
        ObjectAnimator objectAnimatorX1 = ObjectAnimator.ofFloat(picTwo , "scaleX" , 1f,100f,1f);
        final AnimatorSet set1 = new AnimatorSet();
        set1.play(objectAnimatorX1).with(objectAnimatorY1);
        set1.setDuration(3000);



        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set1.start();
            }
        });

        set1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set.start();
            }
        });

    }




}
